import 'package:flutter/material.dart';
import 'package:flutter_pink_app/style/text.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
import "../style/decoration.dart";

// 主页轮播图组合
class HomePageSwiper extends StatelessWidget {
  HomePageSwiper(this._list,
      {this.padding = const EdgeInsets.all(0),
      this.onTapped,
      this.height,
      this.width,
      this.isLoop = true,
      this.imageRadius = 6.0,
      this.floatTextList = const [],
      this.autoPlay = true,
      this.setPaginationShadow = false,
      this.imgfit = BoxFit.cover,
      this.floatTextPadding =
          const EdgeInsets.symmetric(horizontal: 10, vertical: 5),
      this.paginationaMargin = const EdgeInsets.only(right: 4, bottom: 5),
      this.paginationSize = 6.0});

  final List<String> _list;
  final EdgeInsets padding; // 内边距
  final EdgeInsets paginationaMargin; // 分页器外边距
  final EdgeInsets floatTextPadding; // 浮动文字内边距
  final ValueChanged<int> onTapped; // 点击事件
  final double imageRadius; // 图像圆角
  final double paginationSize; // 分页器大小
  final double height; // 分页器大小
  final double width; // 分页器大小
  final bool setPaginationShadow; // 分页器底部阴影
  final List<String> floatTextList; // 浮动文字
  final BoxFit imgfit; // 图片适应方式
  final bool autoPlay; // 是否自动播放
  final bool isLoop; // 是否自动自动循环

  // 构建图像方法
  _buildImage(int index) {
    return ClipRRect(
        borderRadius: BorderRadius.all(Radius.circular(imageRadius)),
        child: Image.network(_list[index], fit: imgfit));
  }

  // 构建通栏方法
  ClipRRect _buildBanner(int index) {
    return ClipRRect(
        borderRadius: BorderRadius.all(Radius.circular(imageRadius)),
        child: Stack(
          fit: StackFit.passthrough,
          children: <Widget>[
            Image.network(_list[index], fit: BoxFit.cover),
            Positioned(
                bottom: 0, left: 0, right: 0, child: _buildFloatText(index))
          ],
        ));
  }

  // 构建浮动文字方法
  Container _buildFloatText(int index) {
    final text = index >= floatTextList.length ? '' : floatTextList[index];// 判断索引是否越界
    return Container(
        decoration: BoxDecoration(gradient: blackLinearGradient()),
        padding: floatTextPadding,
        child: Text(text, style: focusWhiteStyle));
  }

  @override
  Widget build(BuildContext swiperContext) {
    // 返回轮播图
    return Container(
      height: height,
      width: width,
      padding: padding,
      child: Swiper(
        itemCount: _list.length,
        autoplay: autoPlay, // 自动播放
        loop: isLoop,
        onTap: onTapped,
        itemBuilder: (swiperContext, index) =>
            setPaginationShadow ? _buildBanner(index) : _buildImage(index),
        pagination: SwiperPagination(
            alignment: Alignment.bottomRight, // 右下对齐
            margin: paginationaMargin,
            builder: DotSwiperPaginationBuilder(
                color: Colors.white60,
                activeColor: Colors.white,
                size: paginationSize,
                activeSize: paginationSize)),
      ),
    );
  }
}
